class_name EvolveDialog extends Control

var pre_evolution = preload("res://游戏/UI/弹窗/转型弹窗/转型item/item/EvolveItem.tscn")
var arr_evolution = load("res://游戏/evolution.json").data

func _ready():
	$"确认弹窗".hide()
	for dic in arr_evolution:
		if dic.is_two:
			continue
		var node = pre_evolution.instantiate()
		node.init(dic, self)
		$"转型区/转型合集/GridContainer".add_child(node)

func showDialog():
	$"确认弹窗".hide()
	ObjectManager.soundManager.play(SoundType.dialog_open)
	for node in $"转型区/转型合集/GridContainer".get_children():
		node.queue_free()
		$"转型区/转型合集/GridContainer".remove_child(node)
	for dic in arr_evolution:
		if dic.is_two:
			continue
		var node = pre_evolution.instantiate()
		node.init(dic, self)
		$"转型区/转型合集/GridContainer".add_child(node)
	updateDetail($"转型区/转型合集/GridContainer".get_child(0))
	await get_tree().create_timer(0.1).timeout
	$AnimationPlayer.play("show")

# 二级转型，幽灵、炮台、沙兵
func showDialog_secondaryEvolve(initial:String):
	for node in $"转型区/转型合集/GridContainer".get_children():
		node.queue_free()
		$"转型区/转型合集/GridContainer".remove_child(node)
	var json_initial = GameUtils.getEvolutionJson(initial)
	var json_secondary = GameUtils.getEvolutionJson(json_initial.二转)
	var node = pre_evolution.instantiate()
	node.init(json_secondary,self)
	$"转型区/转型合集/GridContainer".add_child(node)
	updateDetail($"转型区/转型合集/GridContainer".get_child(0))
	$AnimationPlayer.play("show")

func _on_背景_gui_input(event):
	if event.is_pressed():
		$AnimationPlayer.play("close")

# 详细说明变化
var last_selectItem = null
func updateDetail(item):
	if last_selectItem != null:
		last_selectItem.cancelSelect()
	item.select()
	last_selectItem = item
	$"转型区/详细说明/图标".texture = item.texture
	$"转型区/详细说明/名字".text = "名字："+item.title
	$"转型区/详细说明/消耗".text = "消耗："+item.cost
	$"转型区/详细说明/视野".text = "视野："+item.视野
	$"转型区/详细说明/动域".text = "动域："+item.动域
	$"转型区/详细说明/被动".text = "被动："+item.被动
	$"转型区/详细说明/技能描述区".update(item.desc) 
	if item.is_two:
		$"转型区/详细说明/二转".text = "我是二转"
	else:
		if item.二转 != "":
			$"转型区/详细说明/二转".text = "二转："+item.二转
		else:
			$"转型区/详细说明/二转".text = "没有二转"
	if item.唯一:
		$"转型区/详细说明/唯一".visible = true
		$"转型区/详细说明/唯一".text = "唯一"
	else:
		$"转型区/详细说明/唯一".visible = false
	if item.唯一:
		$"转型区/详细说明/唯一".visible = true
		$"转型区/详细说明/唯一".text = "唯一"
	else:
		$"转型区/详细说明/唯一".visible = false

func _on_转型_pressed(): 
	ObjectManager.soundManager.play(SoundType.button_click)
	if !GameUtils.checkIsReadyAction():
		return
	if GameUtils.isOnlyEvolution(last_selectItem.json.title) && GameUtils.checkHasThisEvolution(last_selectItem.json.title):
		ObjectManager.toast.showToast("已经有该转型体了")
		return
	if GameUtils.checkEvolveLimitReached(last_selectItem.json.title):
		ObjectManager.toast.showToast("转型失败，不能超过上限")
		return
	if last_selectItem.json.is_two && !GameUtils.checkIsCanSecondEvolve(last_selectItem.json.title):
		return
	var cost = last_selectItem.json.cost.to_int()
	if !GameUtils.checkActionEnough(cost):
		return
	var gameManager = ObjectManager.gameManager as GameManager
	var operationManager = gameManager.operationManager as OperationManager
	var selectPoint = operationManager.selectedPoint as Point
	if GameUtils.checkBigHasSon(selectPoint.big):
		showConfirmDialog()
		return
	WebSocketManager.sendObj(true,"evolve",{"evolution":last_selectItem.json.title,"id":selectPoint.id})
	$AnimationPlayer.play("close")

func showConfirmDialog():
	$"确认弹窗".popup_centered()
	
func _on_确认弹窗_confirmed() -> void:
	var gameManager = ObjectManager.gameManager as GameManager
	var operationManager = gameManager.operationManager as OperationManager
	var selectPoint = operationManager.selectedPoint as Point
	WebSocketManager.sendObj(true,"evolve",{"evolution":last_selectItem.json.title,"id":selectPoint.id})
	$AnimationPlayer.play("close")

func _on_确认弹窗_canceled() -> void:
	$"确认弹窗".hide()
